package com.itheima.Test;

import com.itheima.po.Customer;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MybatisTest {
    //测试姓名和工作的customer
    @Test
    public void findCustomerByNameAndJobs1() throws Exception {
      SqlSession session= MybatisUtils.getSession();
      Customer cus=new Customer();
      //cus.setId(2);
      cus.setUsername("jack");
      cus.setJobs("teacher");
        List<Customer> customers = session.selectList("com.itheima.Mapper.CustomerMapper.findCustomerByNameAndJobs1", cus);
        for (Customer customer2 : customers) {
            System.out.println(customer2);
        }
        session.close();
    }
    @Test
    public void findCustomerByNameAndJobs2() throws Exception{
            SqlSession session= MybatisUtils.getSession();
            Customer cus=new Customer();
            //cus.setId(2);
            cus.setUsername("jack");
            cus.setJobs("teacher");
            List<Customer> customers = session.selectList("com.itheima.Mapper.CustomerMapper.findCustomerByNameAndJobs2", cus);
            for (Customer customer2 : customers) {
                System.out.println(customer2);
            }
            session.close();
    }
    @Test
        public void findCustomerByNameAndJobs3() throws Exception{
            SqlSession session= MybatisUtils.getSession();
            Customer cus=new Customer();
            //cus.setId(2);
            cus.setUsername("jack");
            cus.setJobs("teacher");
            List<Customer> customers = session.selectList("com.itheima.Mapper.CustomerMapper.findCustomerByNameAndJobs3", cus);
            for (Customer customer2 : customers) {
                System.out.println(customer2);
            }
            session.close();
        }
    @Test
    public void findCustomerByNameAndJobs4() throws Exception{
        SqlSession session= MybatisUtils.getSession();
        Customer cus=new Customer();
        //cus.setId(2);
        cus.setUsername("jack");
        cus.setJobs("teacher");
        List<Customer> customers = session.selectList("com.itheima.Mapper.CustomerMapper.findCustomerByNameAndJobs4", cus);
        for (Customer customer2 : customers) {
            System.out.println(customer2);
        }
        session.close();
    }
    @Test
    public void updateCustomer() throws Exception{
        SqlSession session= MybatisUtils.getSession();
        Customer cus=new Customer();
        cus.setId(4);
        cus.setPhone("000000");
        int rows= session.update("com.itheima.Mapper.CustomerMapper.findCustomerByNameAndJobs4", cus);
        if (rows>0){
            System.out.println("成功修改了"+rows+"条数据");
        }else {
            System.out.println("修改失败！");
        }
        session.commit();
        session.close();
    }
    @Test
    public void findCustomerByIdsTest() throws Exception{
        SqlSession session= MybatisUtils.getSession();
        List<Integer> ids=new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);

        List<Customer> customers= session.selectList("com.itheima.Mapper.CustomerMapper.findCustomersById", ids);
       for(Customer customer:customers){
           System.out.println(customer);
       }
        session.close();
    }

    @Test
    public void findCustomerByName() throws Exception{
        SqlSession session= MybatisUtils.getSession();
       Customer customer=new Customer();
       customer.setUsername("j");

        List<Customer> customers= session.selectList("com.itheima.Mapper.CustomerMapper.findCustomersByName",customer);
        for(Customer cus:customers){
            System.out.println(cus);
        }
        session.close();
    }
}